export * from "./def";
import { ImplFromat, BaseFormat, def } from "./def";


class Format extends BaseFormat implements ImplFromat {
  constructor () { super(def); }

  async user(p?: FormatParam<sql.user>): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.user(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.user(p.data);
      return p;
    } else if (p) {
      this.setDef(p, def.user);
      if (typeof p.permission == "string") {
        p.permission = new Uint8Array(uni.base64ToArrayBuffer(p.permission));
      }
      this.setTime(p);
      this.setEntity(p, "user", "pid", "_pid");
      p.money = parseInt(p.money) || 0;
      return p;
    }
  };
  async setting(p?: FormatParam<sql.setting>): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.setting(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.setting(p.data);
      return p;
    } else if (p) {
      this.setDef(p, def.setting);

      return p;
    }
  };
  /** 脚本 */
  async script(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.script(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.script(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.script);
      this.setTime(p);
      return p;
    }
  };
  /** 交易日志 */
  async moneyLog(p?: any): Promise<any> {
    if (Array.isArray(p)) {
      await Promise.all(p.map(async (v) => { await this.moneyLog(v); }));
      return p;
    } else if (this.isPages(p)) {
      await this.moneyLog(p.data);
      return p;
    } else if (p) {
      this.setDef(p, this.def.moneyLog);
      return p;
    }
  };
}
export const format = new Format();
